package ${basePackage}.cli.command;


import cn.hutool.core.bean.BeanUtil;
import ${basePackage}.generator.FileGenerator;
import ${basePackage}.model.DataModel;
import lombok.Data;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine;

import java.util.concurrent.Callable;


<#--生成选项-->
<#macro generateOption modelInfo indent="    " >
    ${indent}@Option(names = {<#if modelInfo.abbr??>"-${modelInfo.abbr}", </#if>"--${modelInfo.fieldName}"}, arity = "0..1", <#if modelInfo.description??>description = "${modelInfo.description}", </#if>interactive = true, echo = true)
    ${indent}private ${modelInfo.type} ${modelInfo.fieldName}<#if modelInfo.defaultValue??> = ${modelInfo.defaultValue?c}</#if>;
</#macro>

<#--生成命令调用-->
<#macro generateCommand modelInfo  indent="     ">
    ${indent}System.out.println("输入${modelInfo.groupName}配置：");
    ${indent}CommandLine commandLine = new CommandLine(${modelInfo.type}Command.class);
    ${indent}commandLine.execute(${modelInfo.allArgsStr});
</#macro>

@Command(name = "generate", mixinStandardHelpOptions = true, version = "generate 1.0",
description = "Generate a new project")
@Data
public class GenerateCommand implements Callable<Integer> {

<#--    生成选项包括分组选项-->
    <#list modelConfig.models as modelInfo>
        <#if modelInfo.groupKey??>

            static DataModel.${modelInfo.type} ${modelInfo.groupKey} = new DataModel.${modelInfo.type}();

            @Command(name = "${modelInfo.groupKey}")
            @Data
            public static class ${modelInfo.type}Command implements Runnable{
                <#list modelInfo.models as modelInfo>
                    <@generateOption modelInfo=modelInfo/>
                </#list>

                @Override
                public void run(){
                    <#list modelInfo.models as subModelInfo>
                    ${modelInfo.groupKey}.${subModelInfo.fieldName} = ${subModelInfo.fieldName};
                    </#list>
                }
            }
        <#else>
            <@generateOption modelInfo=modelInfo/>
        </#if>
    </#list>

<#--    生成调用方法-->
    @Override
    public Integer call() throws Exception {

        <#list modelConfig.models as modelInfo>
            <#if modelInfo.groupKey??>
                <#if modelInfo.condition??>
                    if(${modelInfo.condition}){
                        <@generateCommand modelInfo=modelInfo/>
                    }
                <#else>
                    <@generateCommand modelInfo=modelInfo/>
                </#if>
            </#if>
        </#list>

<#--        填充数据模型对象-->
        DataModel model = new DataModel();
        BeanUtil.copyProperties(this,model);
        <#list modelConfig.models as modelInfo>
            <#if modelInfo.groupKey??>
                model.${modelInfo.groupKey} = ${modelInfo.groupKey};
            </#if>
        </#list>
        FileGenerator.doGenerate(model);
        return 0;
    }
}
